*-------------------------------------------------------------------------------------------------------------*
* The Causal Effects of R&D Grants: Evidence from a Regression Discontinuity (Santoleri et al., 2020)
*-------------------------------------------------------------------------------------------------------------*

/// Table 7: Spillovers on entrepreneurship

* import data on business entry from Eurostat
import excel "$raw_data_path/bd_hgnace2_r3_NACE.xls", sheet("Sheet1") firstrow clear

sort GEO

destring D E F G H I J, replace
rename D y2012
rename E y2013
rename F y2014
rename G y2015
rename H y2016
rename I y2017
rename J y2018

* generate NACE * NUTS ids
egen nace_nuts_id = group(NACE_R2 GEO)

* reshape data
reshape long y, i(nace_nuts_id) j(year_smei)

* rename vars
rename GEO nuts3code
rename y entry_rate

* compute vars
xtset nace_nuts_id year
bys nace_nuts_id: gen lag_entry_rate = ln(l1.entry_rate)
bys nace_nuts_id: gen lead_entry_rate = ln(f1.entry_rate)
bys nace_nuts_id: gen avg_lead_entry_rate = (f1.entry_rate + f2.entry_rate) / 2
bys nace_nuts_id: gen gr_entry_rate = ln(f1.entry_rate) - ln(l1.entry_rate)
bys nace_nuts_id: gen lag_gr_entry_rate = l1.gr_entry_rate

save "$raw_data_path/bd_hgnace2_r3_NACE.dta", replace


* merge with original data
use $data_path/DATA_ALL_FINAL.dta, clear

* keep only Nuts3 code
gen nuts3code = substr(nuts3,1,5)

sort nuts3code
order nuts3code nuts3 

* add macro-industries (https://ec.europa.eu/eurostat/documents/3859598/5902521/KS-RA-07-015-EN.PDF p. 44)
gen NACE_R2 = "Industry (except construction)" if nace2 >=5&nace2<=39
replace NACE_R2 = "Construction" if nace2 >=41&nace2<=43
replace NACE_R2 = "Wholesale and retail trade; repair of motor vehicles and motorcycles" if nace2 >=45&nace2<=47
replace NACE_R2 = "Transportation and storage" if nace2 >=49&nace2<=53
replace NACE_R2 = "Accommodation and food service activities" if nace2 >=55&nace2<=56
replace NACE_R2 = "Information and communication" if nace2 >=58&nace2<=63
replace NACE_R2 = "Financial and insurance activities; real estate activities except activities of holding companies" if nace2 >=64&nace2<=68
replace NACE_R2 = "Professional. scientific and technical activities; administrative and support service activities" if nace2 >=69&nace2<=82
replace NACE_R2 = "Education; human health and social work activities" if nace2 >=85&nace2<=88
replace NACE_R2 = "Arts. entertainment and recreation; other service activities"  if nace2 >=90&nace2<=96

* merge data
merge m:1 nuts3code year_smei NACE_R2 using "$raw_data_path/bd_hgnace2_r3_NACE.dta"

* controls for polinomyal orders on both sides of the threshold
global linear    c.rank#i.award

gen log_entry_rate = ln(entry_rate)

cap drop sample
reghdfe log_entry_rate lag_gr_entry_rate gr_entry_rate award $linear if phase_smei==2, a(competition nace_nuts_id) cluster(nace_nuts_id)
gen sample = e(sample)

* regression reproducing Table 7
est clear
eststo: reghdfe log_entry_rate award $linear if phase_smei==2 & sample==1, a(year_smei nace_nuts_id) cluster(nuts3code nace2)
eststo: reghdfe log_entry_rate award $linear if phase_smei==2 & sample==1 & inrange(rank, -10,10), a(year_smei nace_nuts_id) cluster(nuts3code nace2)
eststo: reghdfe lag_gr_entry_rate award $linear if phase_smei==2 & sample==1, a(year_smei nace_nuts_id) cluster(nuts3code nace2)
eststo: reghdfe lag_gr_entry_rate award $linear if phase_smei==2 & sample==1 & inrange(rank, -10,10), a(year_smei nace_nuts_id) cluster(nuts3code nace2)
eststo: reghdfe gr_entry_rate award $linear lag_gr_entry_rate if phase_smei==2  & sample==1, a(year_smei nace_nuts_id) cluster(nuts3code nace2)
eststo: reghdfe gr_entry_rate award $linear lag_gr_entry_rate if phase_smei==2  & sample==1 & inrange(rank, -10,10), a(year_smei nace_nuts_id) cluster(nuts3code nace2)
esttab est* , replace star(* 0.10 ** 0.05 *** 0.01 ) b(3) se(3) stats(N r2 bic, fmt(0 2 2) labels(N "R-squared" "BIC")) keep(award)
esttab est* using $output/Table_7.tex, replace star(* 0.10 ** 0.05 *** 0.01 ) b(3) se(3) keep(award) stat(N r2 bic, fmt(0 2)) title(The effects on firm creation) mtitles(All 10 All 10 All 10 ) ///
			 coeflabels(award "Grant") indicate( "Rank x Grant=*.award#c.rank" ) varwidth(25) 

			 
			 
* regression reproducing Appendix Table D7			 
est clear
eststo: reghdfe log_entry_rate seal $linearrank if phase_smei==2 & sample==1 &  (award!=1 | seal==1) & sumseal==1, a(year_smei nace_nuts_id) cluster(nuts3code nace2)
eststo: reghdfe log_entry_rate seal $linearrank if phase_smei==2 & sample==1 & inrange(rank, -10,10) &  (award!=1 | seal==1) & sumseal==1, a(year_smei nace_nuts_id) cluster(nuts3code nace2)
eststo: reghdfe lag_gr_entry_rate seal $linearrank if phase_smei==2 & sample==1 &  (award!=1 | seal==1) & sumseal==1, a(year_smei nace_nuts_id) cluster(nuts3code nace2)
eststo: reghdfe lag_gr_entry_rate seal $linearrank if phase_smei==2 & sample==1 & inrange(rank, -10,10) &  (award!=1 | seal==1) & sumseal==1, a(year_smei nace_nuts_id) cluster(nuts3code nace2)
eststo: reghdfe gr_entry_rate seal $linearrank lag_gr_entry_rate if phase_smei==2  & sample==1 &  (award!=1 | seal==1) & sumseal==1, a(year_smei nace_nuts_id) cluster(nuts3code nace2)
eststo: reghdfe gr_entry_rate seal $linearrank lag_gr_entry_rate if phase_smei==2  & sample==1 & inrange(rank, -10,10) &  (award!=1 | seal==1) & sumseal==1, a(year_smei nace_nuts_id) cluster(nuts3code nace2)
esttab est* , replace star(* 0.10 ** 0.05 *** 0.01 ) b(3) se(3) stats(N r2 bic, fmt(0 2 2) labels(N "R-squared" "BIC")) keep(seal)
esttab est* using $output/Table_D7.tex, append star(* 0.10 ** 0.05 *** 0.01 ) b(3) se(3) keep(seal) stat(N r2 bic, fmt(0 2)) title(The effects on firm creation) mtitles(All 10 All 10 All 10 ) ///
			 coeflabels(award "Grant") indicate( "Rank x Grant=*.seal#c.sealrank" ) varwidth(25) 
			 
			 
			 
